/**
 * ·@format·
 *
 * @format
 */

import * as crypto from 'crypto'
const iv = Buffer.alloc(16)

/**
 * 解密
 * @param dataStr {string}
 * @return {string}
 */
export const decrypt = (dataStr: string, key: string): string => {
  const cipherChunks = []
  key = crypto.createHash('md5').update(key, 'utf8').digest('base64')
  const secret = Buffer.from(key, 'binary')
  const decipher = crypto.createDecipheriv('aes-192-cbc', secret, iv)
  decipher.setAutoPadding(true)
  cipherChunks.push(decipher.update(dataStr, 'base64', 'utf8'))
  cipherChunks.push(decipher.final('utf8'))
  return cipherChunks.join('')
}

/**
 * 加密
 * @param dataStr {string}
 * @return {string}
 */
export const encrypt = (dataStr: string, key: string): string => {
  const cipherChunks = []
  key = crypto.createHash('md5').update(key, 'utf8').digest('base64')
  const secret = Buffer.from(key, 'binary')
  const cipher = crypto.createCipheriv('aes-192-cbc', secret, iv)
  cipher.setAutoPadding(true)
  cipherChunks.push(cipher.update(dataStr, 'utf8', 'base64'))
  cipherChunks.push(cipher.final('base64'))
  return cipherChunks.join('')
}
